package EGS.impl;
import java.util.List;
import java.util.concurrent.ExecutorService;

import org.dom4j.Document;
import org.dom4j.Element;

import EGS.EGS;
import TOOLS.SQLtool;
import TOOLS.errCodeManagement;
import WORKPLACE.m_resultData;
import WORKPLACE.stationClient;

public class EGS_execute_service560Impl{
	public boolean isUnknowCase = false;
	ExecutorService iExecutorService;
	Document MSG;
	boolean isPanel = false;
	stationClient iClient=null;
	SQLtool sql = null;
	/**
	 * 560 整托盘点胶过站，记录胶水静止时间为测量值,并记录托盘产品关系
	 */
	public EGS_execute_service560Impl(stationClient nClient, Document MSG_in, ExecutorService executorService, 
			SQLtool goldenSql) throws Exception {
		super();
		sql=goldenSql;
		if(nClient!=null) {
			iClient=nClient;
		}else {
			iClient=null;
		}
		MSG = MSG_in;
		iExecutorService = executorService;
	}

	public Document runCal() {
		String msgClass = "";
		try {
			if (MSG == null) {
				return null;
			}
			Element Mroot = MSG.getRootElement();
			msgClass = Mroot.element("header").attribute("messageClass").getValue();
			String transactionID = Mroot.element("header").attribute("transactionID").getValue();
			String iStation;

			iStation = Mroot.element("header").element("location").attribute("equipmentName").getValue();

			if(msgClass.equals("560")){//整托盘点胶过站，记录胶水静止时间为测量值,并记录托盘产品关系
				Element elePallet=Mroot.element("body").element("pallet");
				String palletID=elePallet.attribute("ID").getValue();
				String stationID=Mroot.element("body").element("station").attribute("ID").getValue();
				stationClient coatingStation=null;
				try {
					coatingStation=EGS.sSheets.getStationClient(stationID);//获取点胶的工位
				}catch(Exception e) {
					coatingStation=null;
				}
				List<?> elePalletSons=elePallet.elements();
				String [] iSNs= null;
				String [] cost= null;
				
				int lenth=elePalletSons.size();
				if(lenth>0) {
					iSNs= new String[lenth];
					cost= new String[lenth];
					for(int i=0;i<lenth;i++) {
						iSNs[i]=((Element)(elePalletSons.get(i))).attribute("sn").getValue();
						cost[i]=((Element)(elePalletSons.get(i))).attribute("seconds").getValue();
					}
				}
				Mroot.element("body").remove(Mroot.element("body").element("pallet"));
				Element eleR=Mroot.element("body").addElement("result");
				try {
					if(lenth>0) {
						String palletInfo="";
						String palletInfo1="";//用于记log
						for(int listI=0;listI<lenth;listI++) {
							m_resultData [] iData=new m_resultData[1];
							iData[0]=new m_resultData();
							iData[0].pos="-1";
							iData[0].name="胶水静置时间";
							iData[0].unit="s";
							iData[0].max="1";
							iData[0].min="0";
							iData[0].value=cost[listI];
							iData[0].result="Pass";
							if(coatingStation!=null) {
								coatingStation.uploadData(iSNs[listI], iData);
								coatingStation.uploadState(iSNs[listI], true,"-1");
							}
							if(listI>0) {
								palletInfo=palletInfo+",";
								palletInfo1=palletInfo1+",";
							}
							palletInfo=palletInfo+"('"+palletID+"','"+iSNs[listI]+"')";
							palletInfo1=palletInfo1+iSNs[listI];
						}
						if(palletInfo.length()>0) {
							sql.Command("delete from [dbo].[resource_pallet] where palletID = '"+palletID+"'");
							sql.Command("insert [dbo].[resource_pallet] (palletID,serialNumber) "
								+ "values "+palletInfo);
						}
						eleR.addAttribute("errorCode", "0");
						eleR.addAttribute("errorText", "Valid");
						eleR.addAttribute("actionCode", "0");
						AKSLogs(5601,""+iStation+"_"+palletID+"_"+palletInfo1);
					}
				} catch (Exception e) {
					String str=errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", transactionID);
					AKSLogs(5600,""+iStation+"_"+palletID+"_"+str);
				}
			} else {
				isUnknowCase = true;
				return null;
			}
			return MSG;
		} catch (Exception e) {
			if(iClient==null) {
				Element Mroot = MSG.getRootElement();
				Element eleR = Mroot.element("body").addElement("result");
				eleR.addAttribute("errorCode", "1");
				eleR.addAttribute("errorText", "工位信息获取失败");
				eleR.addAttribute("actionCode", "0");
				return MSG;
			}
			String str = errCodeManagement.getErrText(e.getMessage());
			System.out.println(str);
			return null;
		}

	}

	public void AKSLogs(int iType, String ilogClientNews) {
		SQLtool.AKSLogs(iType, ilogClientNews);
	}

}
